package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.O2oAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.O2oModel;
import com.banshuibao.bsb_android.model.RequestBodyModel;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshBase;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshListView;
import com.banshuibao.bsb_android.util.IntentManager;

import java.util.List;

public class SpeciaConsultActivity extends BaseActivity implements PullToRefreshBase.OnRefreshListener2, AdapterView.OnItemClickListener {

    private PullToRefreshListView pullToRefreshListView;

    private ListView listView;

    private O2oAdapter adapter;

    private int startId, endId;

    private boolean isUp = false;

    private int row;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_specia_consult);
        initView();
        request();
    }

    public void initView() {
        setTitleText("一对一咨询");
        pullToRefreshListView = findViewById(R.id.ptrlv);
        pullToRefreshListView.setOnRefreshListener(this);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        listView = pullToRefreshListView.getRefreshableView();
        adapter = new O2oAdapter(this);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    public void request() {
        isUp = false;
        startId = 1;
        endId = startId + 9;
        if (adapter != null && adapter.getCount() > 10) {
            endId = adapter.getCount();
        }
        getData();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        request();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        isUp = true;
        endId = startId + 9;
        getData();
    }

    public void getData() {
        RequestBodyModel requestBodyModel = new RequestBodyModel();
        requestBodyModel.setSinceId(startId);
        requestBodyModel.setMaxId(endId);
        HttpUtil.doPost(Constants.Url.COUNSELOR_LIST, requestBodyModel, new HttpResponse(this, O2oModel.class) {
            @Override
            public void onResult(Object obj) {
                O2oModel model = (O2oModel) obj;
                List<O2oModel.DataBean> data = model.getData();
                row = model.getRows();
                startId += row;
                if (isUp) {
                    adapter.setUpData(data);
                } else {
                    adapter.setDownData(data);
                }

            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                pullToRefreshListView.onRefreshComplete();
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (adapter != null) {
            O2oModel.DataBean dataBean = adapter.getItem(position - 1);
            if (dataBean != null) {
                IntentManager.goPersonPageActivity(context, dataBean.getUuid());
            }
        }
    }
}
